<template>
    <el-dialog
        :visible.sync="dialogVisible"
        title="地区管理"
        :close-on-press-escape="false"
        :close-on-click-modal="false"
        width="600px"
    >
        <el-form ref="formRef" :model="formData" :rules="rules" label-width="80px">
            <el-form-item label="编码" prop="code">
                <el-input v-model="formData.code"/>
            </el-form-item>
            <el-form-item label="名称" prop="name">
                <el-input v-model="formData.name"/>
            </el-form-item>
            <el-form-item label="级别" prop="level" size="mini">
                <el-select v-model="formData.level" placeholder="请选择">
                    <el-option key="province" label="省" value="province"/>
                    <el-option key="city" label="市" value="city"/>
                    <el-option key="district" label="区/县" value="district"/>
                    <el-option key="street" label="街道/镇" value="street"/>
                </el-select>
            </el-form-item>
            <el-form-item label="经度" prop="lng">
                <el-input v-model="formData.lng"/>
            </el-form-item>
            <el-form-item label="纬度" prop="lat">
                <el-input v-model="formData.lat"/>
            </el-form-item>
        </el-form>

        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false" style="margin-right: 20px;">取 消</el-button>
            <el-popconfirm title="确定保存吗？" @onConfirm="submitForm()">
                <el-button slot="reference" type="primary">保存</el-button>
            </el-popconfirm>
        </div>
    </el-dialog>
</template>

<script>
import {RegionApi} from '@/api/system/RegionApi'
import {Common} from '@/utils/common'

export default {
    name: 'regionUpdate',
    components: {},
    data() {
        return {
            formData: {
                id: '',
                code: '',
                parentCode: 0,
                name: '',
                level: '',
                lng: '',
                lat: ''
            },
            rules: {
                code: [
                    {required: true, message: '请输入地址编码', trigger: 'blur'},
                ],
                name: [
                    {required: true, message: '请输入地址名称', trigger: 'blur'},
                    {min: 1, max: 60, message: '长度在 1 到 60 个字符', trigger: 'blur'}
                ],
                level: [
                    {required: true, message: '请选择级别', trigger: 'blur'},
                ]
            },
            dialogVisible: false
        }
    },
    methods: {
        // 显示组件
        showUpdate(data) {
            this.dialogVisible = true
            this.$nextTick(() => {
                if (this.$refs['formRef'] !== undefined) {
                    this.$refs['formRef'].resetFields()
                }

                if (data.id > 0) {
                    this.formData = Common.assign(this.formData, data)
                } else {
                    this.formData.id = ''
                    this.formData.parentCode = data.parentCode
                }
            })
        },

        // 提交表单
        submitForm() {
            const that = this
            that.$refs['formRef'].validate((valid) => {
                if (valid) {
                    if (that.formData.id > 0) {
                        RegionApi.edit(that.formData)
                            .then((response) => {
                                that.dialogVisible = false
                                that.$emit('refresh', that.formData.parentCode)
                            })
                    } else {
                        RegionApi.create(that.formData)
                            .then((response) => {
                                that.dialogVisible = false
                                that.$emit('refresh', that.formData.parentCode)
                            })
                    }
                }
            })
        }
    }
}
</script>
